<template>
  <div class="bind_alipay">
    <headTop :headClass="'white_header'">Alipay binding</headTop>
    <div class="infomation">
      <div class="info">
        <span>Alipay</span>
        <input type="text" placeholder="Please enter Alipay account number." v-model="aliAccount">
      </div>
      <div class="info">
        <span>Real name</span>
        <input type="text" placeholder="Please input your real name." :disabled="!!trueName" v-model="trueName" @focus="whiteName">
      </div>
      <div class="info">
        <span>Cell-phone number</span>
        <span>{{phone ? HidePhone(phone) : phone}}</span>
      </div>
      <div class="info send_code">
        <label for="verify" class="verify">
          <span>Code</span>
          <input type="number" id="verify" placeholder="Please enter SMS verification code" v-model="verify">
        </label>
        <span :class="{'no_send' : !isSend, 'is_send' : isSend}" @click="sendCode">{{sendText}}</span>
      </div>
    </div>
    <div class="confirm">
      <span class="public_btn" @click="submit">Sure</span>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  name: 'bindAlipay',
  data () {
    return {
      aliAccount: '',
      verify: '',
      sendText: 'Get code',
      isSend: false,
      codeTime: 60
    };
  },
  computed: {
    ...mapState([
      'trueName',
      'phone'
    ])
  },
  methods: {
    // Submission binding
    submit () {
      if (!this.aliAccount) {
        this.Toast('Please enter Alipay account number.');
      } else if (!this.trueName) {
        this.Toast('Please input your real name.');
      } else if (!this.verify) {
        this.Toast('Please input verification code.');
      } else {
        this.Modal('Cannot be changed after binding.', 'Alipay can not be changed after binding. Please confirm whether or not it is bound.', ['cancel', 'Sure'], () => {
          this.goBind();
        });
      }
    },
    // Send verification code
    sendCode () {
      if (this.isSend) {
        return false;
      } else {
        this.getCode();
      }
    },
    // Count down
    timer () {
      if (this.codeTime > 0) {
        this.codeTime--;
        this.sendText = `(${this.codeTime})s`;
        setTimeout(this.timer, 1000);
      } else {
        this.codeTime = 60;
        this.sendText = 'Recapture';
        this.isSend = false;
      }
    },
    // Binding request
    goBind () {
      this.axios.post('index/rename/apliay', {
        apliay: this.aliAccount,
        code: this.verify,
        username: this.trueName
      }).then(({data}) => {
        if (data.status === 200) {
          this.Toast('Binding success');
          this.$router.go(-1);
        } else {
          this.Toast(data.message);
        }
      });
    },
    // Fill in the real name
    whiteName () {
      if (!this.trueName) {
        this.Modal('Real name authentication', 'You do not have real name authentication yet., Please authenticated with real name first.', ['cancel', 'To authenticate'], [
          () => {
            this.$router.go(-1);
          }, () => {
            this.$router.push('certification');
          }
        ]);
      }
    },
    // Sending verification code request
    getCode () {
      this.axios.post('index/sms/send', {
        phone: this.phone,
        type: 6
      }).then(({data}) => {
        if (data.status === 200) {
          this.isSend = true;
          this.timer();
        } else {
          this.Toast(data.message);
        }
      });
    }
  }
};
</script>

<style lang="scss">
  @import '../style/mixin.scss';
  .bind_alipay{
    padding-top: 108px;
    background-color: #fff;
    .infomation{
      width: 100%;
      padding: 0 30px;
      position: relative;
      margin-bottom: 100px;
      .info{
        width: 100%;
        height: 108px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #e6e6e6;
        font-size: 28px;
        color: #282828;
        input{
          flex: 1;
          height: 100%;
          font-size: 26px;
          color: #282828;
          text-align: right;
          &:placeholder{
            font-size: 26px;
            color: #999;
          }
        }
        &.send_code{
          .verify{
            width: 76%;
            height: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            input{
              flex: 1;
              height: 100%;
            }
          }
          >span{
            width: 24%;
            font-size: 26px;
            color: $color;
            text-align: right;
            &.is_send{
              color: #666;
            }
          }
        }
        &:nth-last-child(1){
          margin-bottom: 0;
        }
      }
      &::before{
        display: block;
        content: '';
        width: 100%;
        height: 20px;
        background-color: #f5f5f5;
        position: absolute;
        left: 0;
        top: -20px;
      }
    }
    .confirm{
      width: 100%;
      text-align: center;
    }
  }
</style>
